import { _decorator, Component, Node, Vec2, Graphics, Color, instantiate, Prefab, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Grid')
export class Grid extends Component {
    @property
    private rows: number = 3;

    @property
    private cols: number = 3;

    @property
    private cellSize: number = 50;

    @property
    private gridColor: Color = new Color(255, 255, 255, 128);

    private graphics: Graphics | null = null;

    onLoad() {
        this.graphics = this.getComponent(Graphics);
        if (!this.graphics) {
            this.graphics = this.addComponent(Graphics);
        }
        this.drawGrid();
    }

    private drawGrid() {
        if (!this.graphics) return;

        this.graphics.clear();
        this.graphics.lineWidth = 5;
        this.graphics.strokeColor = this.gridColor;

        const totalWidth = this.cols * this.cellSize;
        const totalHeight = this.rows * this.cellSize;
        const offsetX = -totalWidth / 2;
        const offsetY = -totalHeight / 2;

        // 绘制水平线
        for (let row = 0; row <= this.rows; row++) {
            const y = offsetY + row * this.cellSize;
            this.graphics.moveTo(offsetX, y);
            this.graphics.lineTo(offsetX + totalWidth, y);
        }

        // 绘制垂直线
        for (let col = 0; col <= this.cols; col++) {
            const x = offsetX + col * this.cellSize;
            this.graphics.moveTo(x, offsetY);
            this.graphics.lineTo(x, offsetY + totalHeight);
        }

        this.graphics.stroke();
    }

    /**
     * 将世界坐标转换为网格坐标
     * @param worldPos 世界坐标
     * @returns 网格坐标
     */
    public worldToGrid(worldPos: Vec2): Vec2 {
        const totalWidth = this.cols * this.cellSize;
        const totalHeight = this.rows * this.cellSize;
        const offsetX = -totalWidth / 2;
        const offsetY = -totalHeight / 2;

        const gridX = Math.floor((worldPos.x - offsetX) / this.cellSize);
        const gridY = Math.floor((worldPos.y - offsetY) / this.cellSize);
        return new Vec2(gridX, gridY);
    }

    /**
     * 将网格坐标转换为世界坐标（返回网格中心点）
     * @param gridPos 网格坐标
     * @returns 世界坐标
     */
    public gridToWorld(gridPos: Vec2): Vec2 {
        const totalWidth = this.cols * this.cellSize;
        const totalHeight = this.rows * this.cellSize;
        const offsetX = -totalWidth / 2;
        const offsetY = -totalHeight / 2;

        const worldX = offsetX + (gridPos.x + 0.5) * this.cellSize;
        const worldY = offsetY + (gridPos.y + 0.5) * this.cellSize;
        return new Vec2(worldX, worldY);
    }

    /**
     * 检查网格坐标是否在有效范围内
     * @param gridPos 网格坐标
     * @returns 是否有效
     */
    public isValidGridPosition(gridPos: Vec2): boolean {
        // return gridPos.x >= 0 && gridPos.x < this.cols &&
        //     gridPos.y >= 0 && gridPos.y < this.rows;
        return true
    }

    /**
     * 在指定网格位置实例化预制体
     * @param gridPos 网格坐标
     * @param prefab 要实例化的预制体
     * @returns 实例化的节点，如果网格坐标无效则返回null
     */
    public instantiateAtGrid(row: number, col: number, prefab: Prefab): Node | null {
        const gridPos = this.getGridPosition(row, col)
        // if (!this.isValidGridPosition(gridPos)) {
        //     return null;
        // }
        const node = instantiate(prefab);
        //node.setParent(this.node);
        node.setWorldPosition(new Vec3(gridPos.x, gridPos.y, 0));
        return node;
    }

    /**
     * 获取网格的行数
     */
    public getRows(): number {
        return this.rows;
    }

    /**
     * 获取网格的列数
     */
    public getCols(): number {
        return this.cols;
    }

    /**
     * 获取单元格大小
     */
    public getCellSize(): number {
        return this.cellSize;
    }

    /**
     * 获取指定网格坐标的世界坐标
     * @param row 行索引
     * @param col 列索引
     * @returns 世界坐标，如果坐标无效则返回null
     */
    public getGridPosition(row: number, col: number): Vec2 | null {
        const gridPos = new Vec2(col, row);
        // if (!this.isValidGridPosition(gridPos)) {
        //     return null;
        // }
        return this.gridToWorld(gridPos);
    }
}